/**
 * MicroStrategy SDK Sample
 *
 * Copyright ? 2011 MicroStrategy Incorporated. All Rights Reserved.
 *
 * MICROSTRATEGY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE 
 * SUITABILITY OF THIS SAMPLE CODE, EITHER EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS 
 * FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. MICROSTRATEGY SHALL NOT 
 * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, 
 * MODIFYING OR DISTRIBUTING THIS SAMPLE CODE OR ITS DERIVATIVES.
 *
 *
 */

package com.amex.uam.task;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.amex.uam.action.CountryRegionAction;
import com.amex.uam.action.MstrAction;
import com.amex.uam.action.ProjAction;
import com.amex.uam.action.UserRegistrationAction;
import com.amex.uam.model.Message;
import com.microstrategy.web.beans.MarkupOutput;
import com.microstrategy.web.beans.RequestKeys;
import com.microstrategy.web.objects.WebIServerSession;
import com.microstrategy.web.tasks.AbstractBaseTask;
import com.microstrategy.web.tasks.TaskException;
import com.microstrategy.web.tasks.TaskParameterMetadata;
import com.microstrategy.web.tasks.TaskRequestContext;

public class PreApprovedTask extends AbstractBaseTask {

	public static final String SID_LOAD_ALL_PROJECTS = "LoadAllProjects";
	public static final String SID_LOAD_SECURITY_GROUP_BY_GROUP_NAME = "LoadSecurityGroupByGroupName";
	public static final String SID_LOAD_GEO_CTRY = "LoadGEOCounrty";
	public static final String SID_LOAD_GEO_RGN = "LoadGEORegion";
	public static final String SID_REGISTER_USERP_ROFILE = "RegisterUserProfile";
	
	static BeanFactory ctx = new ClassPathXmlApplicationContext("beans.xml");
	
	private TaskParameterMetadata taskSubID;
	
	public PreApprovedTask() {
		super("PreApprovedTask");
		taskSubID = addParameterMetadata("TaskSubID", "Enter Operation", true, "");
	}

	@Override
	public void processRequest(TaskRequestContext context, MarkupOutput markupOutput) throws TaskException {
		
		RequestKeys keys = context.getRequestKeys();
		String subId = keys.getValue(taskSubID.getName());

		WebIServerSession session = null;
		try {
			session = context.getWebIServerSession(null, "session");
		} catch (Throwable t) {
		}

		Message message = null;

		System.out.println("** PreApprovedTask->taskSub: " + subId);
		
		if (SID_LOAD_ALL_PROJECTS.equalsIgnoreCase(subId)) {
			message = ctx.getBean("projAction", ProjAction.class).loadAllProjects();
		} else if (SID_LOAD_SECURITY_GROUP_BY_GROUP_NAME.equalsIgnoreCase(subId)) {
			message = ctx.getBean("mstrAction", MstrAction.class).loadSecurityGroupByGroupName(keys, session);
		} else if(SID_LOAD_GEO_CTRY.equalsIgnoreCase(subId)){
			message = ctx.getBean("countryRegionAction", CountryRegionAction.class).loadGEOCounrty();
		}else if(SID_LOAD_GEO_RGN.equalsIgnoreCase(subId)){
			message = ctx.getBean("countryRegionAction", CountryRegionAction.class).loadGEORegion();
		} else if(SID_REGISTER_USERP_ROFILE.equalsIgnoreCase(subId)){
			message = ctx.getBean("userRegistrationAction", UserRegistrationAction.class).registerUser(keys);
		} 
		
		if (message != null) {
			//System.out.println(message.toJSON());
			markupOutput.append(message.toJSON());
		} else {
			System.out.println("Nothing respond!");
			markupOutput.append("");
		}
	}

}